using System;
using System.Collections.Generic;
using System.Text;
using VSSDKAssist.Assets.Actions.Base;
using System.Resources;
using VSSDKAssist.Recipes.Common.Actions;
using EnvDTE;
using Microsoft.Practices.RecipeFramework;
using VSSDKAssist.Recipes.Common.Helpers;

namespace VSSDKAssist.Recipes.Common.Actions
{
	/// <summary>
	/// Action that adds a text entry to a resx file
	/// </summary>
	public class AddTextEntryToResxAction : ActionBase
	{
		private ProjectItem resxProjectItem;

		/// <summary>
		/// Gets or sets the RESX project item.
		/// </summary>
		/// <value>The RESX project item.</value>
		[Input(Required = true)]
		public ProjectItem ResxProjectItem
		{
			get { return resxProjectItem; }
			set { resxProjectItem = value; }
		}

		private string text;

		/// <summary>
		/// Gets or sets the text.
		/// </summary>
		/// <value>The text.</value>
		[Input(Required = true)]
		public string Text
		{
			get { return text; }
			set { text = value; }
		}

		private string textResourceId;

		/// <summary>
		/// Gets or sets the text resource id.
		/// </summary>
		/// <value>The text resource id.</value>
		[Input(Required = true)]
		public string TextResourceId
		{
			get { return textResourceId; }
			set { textResourceId = value; }
		}

		/// <summary>
		/// Called when [execute].
		/// </summary>
		protected override void OnExecute()
		{
			string resxFileName =
				this.ResxProjectItem.Properties.Item("FullPath").Value.ToString();

			ResXDataNode node = ResxHelper.GetResxDataNodeById(
				this.Site as IServiceProvider,
				resxFileName,
				this.TextResourceId);

			if(node != null)
			{
				ResXDataNode newNode = new ResXDataNode(this.textResourceId, this.text);

				ResxHelper.UpdateResxEntry(
					this.ResxProjectItem,
					newNode,
					this.TextResourceId);
			}
			else
			{
				ResxHelper.AppendTextResxEntry(
					this.ResxProjectItem,
					this.TextResourceId,
					this.Text);
			}
		}
	}
}